package kt.com.bilin.coin.ui.activity

import android.animation.ValueAnimator
import android.view.View
import android.widget.ImageView
import com.alibaba.android.arouter.facade.annotation.Route
import com.bilin.coin.R
import kotlinx.android.synthetic.main.activity_lucky_turntable.*
import kt.com.bilin.coin.api.API_SERVICE
import kt.com.bilin.coin.api.ApiObserver
import kt.com.bilin.coin.api.toData
import kt.com.bilin.coin.bean.LotteryAwardBean
import kt.com.bilin.coin.bean.LuckyTurntableEnum
import kt.com.bilin.coin.common.*
import kt.com.bilin.coin.ui.dialog.showAlertDialog
import kt.com.bilin.coin.ui.widget.wheelsurf.listener.RotateListener
import kt.com.bilin.coin.utils.KotlinTransferJava
import java.util.*

@Route(path = PATH_LUCKY_TURNTABLE)
class LuckyTurntableActivity : BaseActivity(R.layout.activity_lucky_turntable) {
    private var isStart = false
    private var mLotteryAwardBean: LotteryAwardBean? = null
    override fun initView() {
        bindViewClick(this, ivLotteryRules, ivMyPrize)
        wsWheelSurfView.setRotateListener(object : RotateListener {
            override fun rotateEnd(position: Int, des: String?) {
                mLotteryAwardBean?.run {
                    showAlertDialog {
                        message(message = awardDesc)
                        positiveButton { }
                    }
                }
                isStart = false
                onReload()
            }

            override fun rotating(valueAnimator: ValueAnimator?) {

            }

            override fun rotateBefore(goImg: ImageView?) {
                // 3,4,5,6
                if (!isStart) {
                    isStart = true
                    launchUI({ API_SERVICE.draw().toData() }, ApiObserver(this@LuckyTurntableActivity, error = { isStart = false }) {
                        if (it != null) {
                            mLotteryAwardBean = it
                            for (value in LuckyTurntableEnum.values()) {
                                if (it.awardId == value.id) {
                                    wsWheelSurfView.startRotate(value.pisition)
                                }
                            }
                        } else {
                            isStart = false
                        }
                    })
                }
            }
        })
    }

    override fun initData() {
        val mMarqueeMessages: MutableList<String> = mutableListOf()
        val phone2 = arrayOf(3, 5, 8, 7)
        val phone3 = arrayOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
        val luckyTurntableEnums = arrayOf(
                LuckyTurntableEnum.THREE,
                LuckyTurntableEnum.FOUR,
                LuckyTurntableEnum.WORK_SCORE,
                LuckyTurntableEnum.FIVE_BLESSINGS_1,
                LuckyTurntableEnum.FIVE_BLESSINGS_2,
                LuckyTurntableEnum.FIVE_BLESSINGS_3,
                LuckyTurntableEnum.FIVE_BLESSINGS_4,
                LuckyTurntableEnum.FIVE_BLESSINGS_5,
        )
        for (index in 0..4) {
            phone2.shuffle()
            phone3.shuffle()
            luckyTurntableEnums.shuffle()
            mMarqueeMessages.add("恭喜用户1${phone2[0]}${phone3[0]}******获得${luckyTurntableEnums[0].luckyName}")
        }
        KotlinTransferJava.marqueeViewStartStrWithList(marqueeView, mMarqueeMessages)
        onReload()
    }

    private fun onReload() {
        launchUI({ tvSurplus.text = getString(R.string.text_lucky_turntable_surplus, API_SERVICE.gtnm().toData()?.reaminNum ?: 0) }, ApiObserver())
    }

    override fun onClick(v: View?) {
        when (v) {
            ivLotteryRules -> openUrl(URL_RULE, "抽奖规则")
            ivMyPrize -> ARouterNavigation(PATH_MY_PRIZE)
        }
    }
}